package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.exception.logicException;
import cn.wolfcode.mapper.ActivityMapper;
import cn.wolfcode.mapper.ChoiceMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.util.UserContextUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ChoiceMapper choiceMapper;


    @Override
    public void save(Activity activity) {
        activityMapper.insert(activity);
    }

    @Override
    public void delete(Long id) {
        activityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Activity activity) {
        activityMapper.updateByPrimaryKey(activity);
    }

    @Override
    public Activity get(Long id) {
        return activityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Activity> listAll() {
        return activityMapper.selectAll();
    }

    @Override
    public PageInfo<Activity> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Activity> activitys = activityMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Activity>(activitys);
    }


    @Override
    public List<ActivityItem> selectActivityItemAll(Long id) {
        return activityMapper.selectActivityItemAll(id);
    }


    @Override
    public void VoteToComplete(Integer optionsRadios) {
        //根据选项ID查出此选项
        ActivityItem activityItem = activityMapper.selectActivityItem(optionsRadios);
        //把选项票数+1
        activityItem.setNum(activityItem.getNum()+1);
        //加1后再保存到表中
        activityMapper.updateNum(activityItem);
        //在中间表添加关系,表示这个员工已经投到票了
        Choice choice = new Choice();
        choice.setEmployee(UserContextUtil.getUserContext());
        choice.setCreateTime(new Date());
        choice.setActivityItem(activityItem);
        choiceMapper.insert(choice);
    }

    @Override
    public Activity selectResult(Long id) {
        //总人数
        int count = 0;
        //
        //查询出这个投票单
        Activity activity = activityMapper.selectByPrimaryKey(id);
        //查询出这个表单的选项
        List<ActivityItem> activityItems = activityMapper.selectActivityItemAll(activity.getId());
        activity.setItems(activityItems);
        //添加总人数
        for (ActivityItem activityItem : activityItems) {
            count += activityItem.getNum();
        }
        activity.setAllNum(count);
        return activity;
    }

    @Override
    public void examine(Long id) {
        //根据用户id 和 投票单id 查询
        Choice choice =  choiceMapper.examine(UserContextUtil.getUserContext().getId(),id);
        if (choice != null ) {
            //投票了抛异常
            throw new logicException("你投过票了");
        }
    }
}
